package com.wgchao.authmanager.query;

import com.wgchao.authmanager.application.vo.AuthoHandleVo;
import com.wgchao.authmanager.application.vo.AuthoInfoVo;
import com.wgchao.authmanager.application.vo.RoleVo;
import com.wgchao.authmanager.infrastructure.jpa.entity.*;
import com.wgchao.authmanager.infrastructure.jpa.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: AuthoQuery
 * @author: wgchao
 * @createTime: 2020/5/9 7:27 PM
 * @Description: 授权查询
 */
@Service
public class AuthoQuery {
    @Autowired
    private UserAuthoInfoJpaRepository uaRepository;
    @Autowired
    private UserAuthoHandleJpaRepository uahRepository;
    @Autowired
    private RoleAuthoInfoJpaRepository raRepository;
    @Autowired
    private RoleAuthoHandleJpaRepository rahRepository;
    @Autowired
    private MenuInfoJpaRepository menuInfoJpaRepository;
    @Autowired
    private HandleInfoJpaRepository handleInfoJpaRepository;
    @Autowired
    private RoleInfoQuery roleInfoQuery;

    /**
     * 获取账户某项目授权信息
     * @param uname
     * @return
     */
    public List<AuthoInfoVo> searchAuthosOfAdmin(String uname, String projectIdentify){
        List<UserAuthoInfo> userAuthoInfos = uaRepository.findAllByUnameAndAndProjectIdentify(uname, projectIdentify);
        if(userAuthoInfos==null){
            return new ArrayList<>();
        }
        List<AuthoInfoVo> authoInfoVos = new ArrayList<>();
        for(UserAuthoInfo userAuthoInfo : userAuthoInfos){
            AuthoInfoVo authoInfoVo = new AuthoInfoVo();
            authoInfoVo.setProjectIdentify(projectIdentify);
            authoInfoVo.setMenuIdentify(userAuthoInfo.getMenuIdentify());
            authoInfoVo.setAuthorize(userAuthoInfo.isAuthorize());
            MenuInfo menuInfo = menuInfoJpaRepository
                    .findFirstByProjectIdentifyAndMenuIdentify(projectIdentify, userAuthoInfo.getMenuIdentify());
            authoInfoVo.setMenuName(menuInfo.getName());
            List<UserAuthoHandleInfo> userAuthoHandleInfos = uahRepository.findAllByUserAuthoId(userAuthoInfo.getId());
            List<AuthoHandleVo> handles = new ArrayList<>();
            for(UserAuthoHandleInfo userAuthoHandleInfo : userAuthoHandleInfos){
                HandleInfo handleInfo = handleInfoJpaRepository.findFirstByIdentify(userAuthoHandleInfo.getHandleIdentify());
                if(handleInfo == null){
                    continue;
                }
                AuthoHandleVo authoHandleVo = new AuthoHandleVo(userAuthoHandleInfo.getHandleIdentify(),
                        handleInfo.getName());
                handles.add(authoHandleVo);
            }
            authoInfoVo.setHandles(handles);
            authoInfoVos.add(authoInfoVo);
        }
        return authoInfoVos;
    }

    /**
     * 获取角色某项目授权信息
     * @return
     */
    public List<AuthoInfoVo> searchAuthosOfRole(String agencyCode, String roleName, String projectIdentify){
        List<RoleAuthoInfo> roleAuthoInfos = raRepository
                .findAllByAgencyCodeAndRoleNameAndProjectIdentify(agencyCode, roleName, projectIdentify);
        if(roleAuthoInfos==null){
            return new ArrayList<>();
        }
        List<AuthoInfoVo> authoInfoVos = new ArrayList<>();
        for(RoleAuthoInfo roleAuthoInfo : roleAuthoInfos){
            AuthoInfoVo authoInfoVo = new AuthoInfoVo();
            authoInfoVo.setProjectIdentify(projectIdentify);
            authoInfoVo.setMenuIdentify(roleAuthoInfo.getMenuIdentify());
            authoInfoVo.setAuthorize(roleAuthoInfo.isAuthorize());
            MenuInfo menuInfo = menuInfoJpaRepository
                    .findFirstByProjectIdentifyAndMenuIdentify(projectIdentify, roleAuthoInfo.getMenuIdentify());
            authoInfoVo.setMenuName(menuInfo.getName());
            List<RoleAuthoHandleInfo> roleAuthoHandleInfos = rahRepository.findAllByRoleAuthoId(roleAuthoInfo.getId());
            List<AuthoHandleVo> handles = new ArrayList<>();
            for(RoleAuthoHandleInfo roleAuthoHandleInfo : roleAuthoHandleInfos){
                HandleInfo handleInfo = handleInfoJpaRepository.findFirstByIdentify(roleAuthoHandleInfo.getHandleIdentify());
                if(handleInfo == null){
                    continue;
                }
                AuthoHandleVo authoHandleVo = new AuthoHandleVo(roleAuthoHandleInfo.getHandleIdentify(),
                        handleInfo.getName());
                handles.add(authoHandleVo);
            }
            authoInfoVo.setHandles(handles);
            authoInfoVos.add(authoInfoVo);
        }
        return authoInfoVos;
    }

    /**
     * 获取账户在项目下所有菜单权限
     * @param uname
     * @return
     */
    public List<AuthoInfoVo> searchAllAuthosOfAdmin(String uname, String projectIdentify){
        //获取账户分配权限
        List<AuthoInfoVo> adminAuthoInfoVos = searchAuthosOfAdmin(uname, projectIdentify);

        //获取账户角色
        List<RoleVo> roleVos = roleInfoQuery.searchRolesOfAdmin(uname);
        if(roleVos!=null && !roleVos.isEmpty()){
            for(RoleVo roleVo : roleVos){
                List<AuthoInfoVo> roleAuthos = searchAuthosOfRole(roleVo.getAgencyCode(), roleVo.getRoleName(), projectIdentify);
                adminAuthoInfoVos = combine(adminAuthoInfoVos, roleAuthos);
            }
        }

        return adminAuthoInfoVos;
    }

    private List<AuthoInfoVo> combine(List<AuthoInfoVo> ori, List<AuthoInfoVo> newVos){
        if(ori == null || ori.isEmpty()){
            return newVos;
        }
        Iterator<AuthoInfoVo> iterator = newVos.iterator();
        while(iterator.hasNext()){
            AuthoInfoVo newVo = iterator.next();
            if(ori.contains(newVo)){
                int index = ori.indexOf(newVo);
                ori.get(index).combine(newVo);
            }else{
                ori.add(newVo);
            }
            iterator.remove();
        }

        return ori;
    }


}
